#pragma once

#include <QWidget>
#include <QFile>
#include <QFileInfo>
#include <QThread>
#include <QDir>
#include <QProcess>
#include <QMovie>
#include "const.h"
#include "httpcommon.h"
#include "ui_downloaditem.h"
#include "pthread.h"
#include "log.h"
#include "globaldata.h"

class DownloadItem : public QWidget
{
    Q_OBJECT

public:
	DownloadItem(QString itemID, QString recordID, QString name, QString status, QString downloadUrl, QWidget *parent = Q_NULLPTR);
	~DownloadItem();
	static void* processDownloadProc(void* args);
	QString status() { return m_status; };
	void download();
	void transcode();
	void cancelDownload();
protected:
	void enterEvent(QEvent *event);
	void leaveEvent(QEvent *event);
	public Q_SLOTS:
	void on_pbDelete_clicked();
	void on_pbPause_clicked();
	void on_pbResume_clicked();
	private Q_SLOTS:
	void refreshProgressbarSlot(int progress);
	void changeStatus(QString status);
	void readFromStdOut();
Q_SIGNALS:
	void changeStatusSignal(QString);
	void refreshProgressbarSignal(int);
	void transcodeDownloadSignal(QString);
	void transcodeFailedDownloadSignal(QString);
	void finishDownloadSignal(QString, QString);
	void pauseDownloadSignal(QString);
	void resumeDownloadSignal(QString);
	void deleteDownloadSignal(QString);
private:
	void init();
	void calcTsTotal();
	void calTsExist();
	void startDownload();
	void stopDownload();
private:
    Ui::DownloadItem ui;
	QString m_itemID;
	QString m_recordID;
	QString m_name;
	QString m_destFileName;
	QString m_status;
	pthread_t m_tidDownloadProcess;
	int m_progress;
	QString m_downloadUrl;
	int m_tsExist;
	int m_tsTotal;
	QStringList m_toDownloadList;
	QString m_downloadTmpRootDir;
	QString m_m3u8Path;
	QString m_downloadTmpDir;
	QProcess *m_transcodeProcess;
	QMovie *m_mv;
};
